package spring.spring_blog.server.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.spring_blog.common.utils.BeanTransUtils;
import spring.spring_blog.common.utils.SecurityUtil;
import spring.spring_blog.mapper.userInfoMapper;
import spring.spring_blog.pojo.model.blogInfo;
import spring.spring_blog.pojo.model.userInfo;
import spring.spring_blog.mapper.*;
import spring.spring_blog.pojo.request.addBlogReq;
import spring.spring_blog.pojo.request.registerReq;
import spring.spring_blog.pojo.request.updateBlogReq;
import spring.spring_blog.pojo.response.blogListInfoResp;
import spring.spring_blog.pojo.response.response;
import spring.spring_blog.pojo.response.userInfoResp;
import spring.spring_blog.server.BlogServer;

import java.util.List;

//左边的用户信息的总数显示错误，显示的是总的博客数而不是用户的博客数 tomorrow
@Service
@Slf4j
public class blogServerImp implements BlogServer {

    @Autowired
    userInfoMapper userInfoMapper;

    @Autowired
    blogInfoMapper blogInfoMapper;

    @Override
    public boolean deleteBlog(int blogId) {
        UpdateWrapper<blogInfo> qw = new UpdateWrapper<>();
        qw.set("delete_flag", 1)
                .eq("id", blogId);

        return blogInfoMapper.update(qw) == 1;
    }

    @Override
    public userInfo login(String username, String password) {
        QueryWrapper wrapper = new QueryWrapper<blogInfo>()
                .eq("user_name", username);
        userInfo userInfo = userInfoMapper.selectOne(wrapper);

        boolean flg = SecurityUtil.verify(password, userInfo.getPassword());
        if (flg) {
            return userInfo;
        }
        return null;
    }

    //注册接口
    @Override
    public response register(registerReq registerReq) {
        //加密
        registerReq.setPassword(SecurityUtil.encrypt(registerReq.getPassword()));

        userInfo u = new userInfo();
        BeanUtils.copyProperties(registerReq, u);

        log.info("registerInfo: " + u);

        userInfoMapper.insert(u);

        return response.success("scc", null);
    }

    @Override
    public List<blogListInfoResp> getAllBlogInfo(int userId) {
        QueryWrapper wrapper = new QueryWrapper<blogInfo>().eq("user_id", userId)
                .eq("delete_flag", 0);

        List<blogInfo> list = blogInfoMapper.selectList(wrapper);

        //list封装为要放回的类型，使用BeanUtils.copyProperties可以对俩个对象中的相同的指端进行赋值
        return list.stream()
                .map(blogInfo -> BeanTransUtils.tran(blogInfo))
                .toList();
//                .collect(Collectors.toList());
    }

    @Override
    public List<blogListInfoResp> getAllBlog() {
        QueryWrapper wrapper = new QueryWrapper<blogInfo>()
                .eq("delete_flag", 0);

        List<blogInfo> list = blogInfoMapper.selectList(wrapper);

        return list.stream()
                .map(blogInfo -> BeanTransUtils.tran(blogInfo))
                .toList();
    }

    public blogInfo getBlogInfoDetail(int id) {
        return blogInfoMapper.selectById(id);
    }


    @Override
    public userInfoResp getUserInfo(Integer userId) {
        QueryWrapper<userInfo> qw = new QueryWrapper<userInfo>().eq("id", userId)
                .eq("delete_flag", 0);
        userInfo u = userInfoMapper.selectOne(qw);

        userInfoResp req = new userInfoResp();
        BeanUtils.copyProperties(u, req);

        log.info("userinfo: {}",req.toString());

        return req;
    }

    @Override
    public response addBlog(addBlogReq l) {
        blogInfo u = new blogInfo();
        BeanUtils.copyProperties(l, u);

        log.info("addBlog: {}",u.toString());

        blogInfoMapper.insert(u);
        return response.success("scc", null);
    }

    @Override
    public response updateBlog(updateBlogReq updateReq) {
        blogInfo u = new blogInfo();

        BeanUtils.copyProperties(updateReq, u);
        UpdateWrapper<blogInfo> blogInfoUpdateWrapper = new UpdateWrapper<blogInfo>()
                .set("title", u.getTitle()).set("content", u.getContent())
                .eq("id", u.getId()).eq("user_id", u.getUserId());

        blogInfoMapper.update(blogInfoUpdateWrapper);
        return response.success("scc", null);
    }



}
